<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;

use app\api\service\Token;
use app\lib\exception\CurdException;
use app\lib\exception\SuccessMessage;
use think\Db;
use think\Exception;

/**
 * 意见反馈表
 * Class Feedback
 * @package app\api\model
 */
class Feedback extends BaseModel
{
    protected $autoWriteTimestamp = true;

    /**
     * @Desc 新增意见反馈
     *
     * @param $param
     *
     * @return SuccessMessage|bool
     */
    public static function insertFeedback($param)
    {
        Db::startTrans();
        try {
            $user_weixin_id = Token::getUidFromCache();
            $image_id = Image::insertImage($param['images']);
            $insertData = [
                'user_weixin_id' => $user_weixin_id,
                'info'           => $param['info'],
                'image_id'       => $image_id,
            ];
            if (!self::create($insertData)) {
                throw new CurdException();
            }
            Db::commit();
            return new SuccessMessage();
        } catch (Exception $e) {
            Db::rollback();
            throw $e;
        }
    }
}